package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatCircle;
import com.happy.cat.project.system.service.ICatCircleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 圈子管理Controller
 *
 * @author verne
 * @date 2020-07-22
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/circle" )
@Api("圈子管理 API")
public class CatCircleController extends BaseController {

    private final ICatCircleService iCatCircleService;

    /**
     * 查询圈子管理列表
     */
    @ApiOperation(value = "查询圈子管理列表",notes = "查询圈子管理列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:circle:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatCircle catCircle)
    {
        startPage();
        LambdaQueryWrapper<CatCircle> lqw = new LambdaQueryWrapper<CatCircle>();
        if (catCircle.getGategoryId() != null){
            lqw.eq(CatCircle::getGategoryId ,catCircle.getGategoryId());
        }
        if (StringUtils.isNotBlank(catCircle.getName())){
            lqw.like(CatCircle::getName ,catCircle.getName());
        }
        if (StringUtils.isNotBlank(catCircle.getCoverImg())){
            lqw.eq(CatCircle::getCoverImg ,catCircle.getCoverImg());
        }
        if (StringUtils.isNotBlank(catCircle.getBackImg())){
            lqw.eq(CatCircle::getBackImg ,catCircle.getBackImg());
        }
        if (StringUtils.isNotBlank(catCircle.getFansGroupIds())){
            lqw.eq(CatCircle::getFansGroupIds ,catCircle.getFansGroupIds());
        }
        if (StringUtils.isNotBlank(catCircle.getNormIds())){
            lqw.eq(CatCircle::getNormIds ,catCircle.getNormIds());
        }
        if (catCircle.getStatus() != null){
            lqw.eq(CatCircle::getStatus ,catCircle.getStatus());
        }
        List<CatCircle> list = iCatCircleService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出圈子管理列表
     */
    @ApiOperation(value = "导出圈子管理列表",notes = "导出圈子管理列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circle:export')" )
    @Log(title = "圈子管理" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatCircle catCircle) {
        LambdaQueryWrapper<CatCircle> lqw = new LambdaQueryWrapper<CatCircle>(catCircle);
        List<CatCircle> list = iCatCircleService.list(lqw);
        ExcelUtil<CatCircle> util = new ExcelUtil<CatCircle>(CatCircle. class);
        return util.exportExcel(list, "circle" );
    }

    /**
     * 获取圈子管理详细信息
     */
    @ApiOperation(value = "获取圈子管理详细信息",notes = "获取圈子管理详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circle:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatCircleService.getById(id));
    }

    /**
     * 新增圈子管理
     */
    @ApiOperation(value = "新增圈子管理",notes = "新增圈子管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circle:add')" )
    @Log(title = "圈子管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatCircle catCircle) {
        catCircle.setCreateBy(SecurityUtils.getUsername());
        catCircle.setCreateTime(new Date());
        return toAjax(iCatCircleService.save(catCircle) ? 1 : 0);
    }

    /**
     * 修改圈子管理
     */
    @ApiOperation(value = "修改圈子管理",notes = "修改圈子管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circle:edit')" )
    @Log(title = "圈子管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatCircle catCircle) {
        return toAjax(iCatCircleService.updateById(catCircle) ? 1 : 0);
    }

    /**
     * 删除圈子管理
     */
    @ApiOperation(value = "删除圈子管理",notes = "删除圈子管理",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:circle:remove')" )
    @Log(title = "圈子管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatCircleService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
